<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改用户信息</title>
</head>
<body>
    <h1>修改用户信息</h1>
    <form>
        <ul type="none" style="padding-left: 0px;">
            <li> 邮箱：<input type="text" name="email" id="email" value=""></li>
            <li><input type="hidden" name="id" id="id" value=""></li>
            <li><input type="button" value="修改" onclick="up3()"></li>
        </ul>
    </form>
    <script>
        function up3(){
            //用户点击修改后调用函数完成数据更新
            //如果更新成功就跳转list.html
            //1、获取用户输入的邮箱地址
            var email = document.getElementById("email");
            //2、获取用户id
            var id = document.getElementById("id");
            //alert(email.value+"|"+id.value);
            //3、创建AJAX对象更新数据
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function(){
                if(xhr.readyState===4&&xhr.status==200){
                    var rs = xhr.responseText;
                    if(rs==1){
                        alert("修改成功");
                        location.href="list.html";
                    }else{
                        alert("修改失败");
                    }
                }
            }
            var url = "http://127.0.0.1:3000/user/update";
            xhr.open("PUT",url,true);
            xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            xhr.send(`email=${email.value}&id=${id.value}`);
        }
        up4();
        function up4(){
            //alert(123);
            //获取浏览器地址栏中的数据
            //var data = location.search;
            //截取字符串电话号码
            //var i2 = data.indexOf("=");
            //var i3 = data.indexOf("&");
            //data =data.slice(i2+1,i3);
            //alert(data);

            //浏览器内部对象获取地址栏中的数据
            var obj =new URLSearchParams(location.search);
            var email = document.getElementById("email");
            email.value = obj.get("email");
            var id =document.getElementById("id");
            id.value = obj.get("id");
        }
    </script>
</body>
</html>